<?php
include_once 'mod.php';
$mod = new mod();
$mysql = $mod->link();
$good_name='';
$class='';
$style = '';
//表格
$sql = "select g.*,c.class_name from goods g inner join classification c on g.class = c.id";
$result = $mod->select($mysql,$sql);

//类型的下拉框选项
$sql1 = "select * from classification";
$result1 = $mod->select($mysql,$sql1);

//搜素
//if (!empty($_GET['search'])){
    $where = ' where';
    if (!empty($_GET['good_name'])){
        $good_name = $_GET['good_name'];
        $sql.= $where." good_name like '%$good_name%'";
        $where = ' and';
    }
    if (!empty($_GET['class'])){
        $class = $_GET['class'];
        $sql.=$where." class = $class ";
        $where = 'and';
    }
    if (!empty($_GET['style'])){
        $style = $_GET['style'];
        $sql.=$where." style = $style ";
        $where = ' and';
    }


//}

//分页

if (!empty($_GET['page'])){
    $page = $_GET['page'];
}else{
    $page = 1;
}
$num = 3;
$begin_num = ($page-1)*$num;
$sql.=" limit $begin_num,$num";
$result = $mod->select($mysql,$sql);

//获取总页数
$sql3 = "select count(*) as num from goods g inner join classification c on g.class = c.id";
$result3 = $mod->select($mysql,$sql3);
$num1= $result3[0]['num'];
$all_page = ceil($num1/$num);

//分页
$base_query = "/web3/index2.php?good_name=$good_name&class=$class&style=$style";

//首页
$firstpage = $base_query."&page=1";
//上一页
$pre_page = $page-1;
if($pre_page<=1){
    $pre_page =1;
}
$prepage = $base_query."&page={$pre_page}";
//下一页
$next_page = $page+1;
if($next_page>=$all_page){
    $next_page=$all_page;
}
$nextpage= $base_query."&page={$next_page}";
//尾页
$lastpage = $base_query."&page={$all_page}";

?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>首页</title>
</head>
<body>
<form action="index2.php" method="get">
    商品名:<input type="text" name="good_name" value="<?=$_GET['good_name']?>">
    类型:<select name="class">
        <option value="">===请选择状态===</option>
        <?php foreach ($result1 as $item){?>
        <option value="<?=$item['id']?>" <?=$item['id']==$_GET['class']? 'selected':''?>><?=$item['class_name']?></option>
        <?php }?>
    </select>
    状态:
        <input type="radio" name="style" value="1" <?=$style==1? 'checked':''?>>在售
        <input type="radio" name="style" value="2" <?=$style==2? 'checked':''?>>售罄
    <input type="submit" name="search">

<table border="1" cellpadding="10px" cellspacing="0">
    <tr>
        <th>id</th>
        <th>商品名</th>
        <th>图像</th>
        <th>类型</th>
        <th>状态</th>
        <th>创造时间</th>
        <th>更新时间</th>
        <th>操作</th>
    </tr>
    <?php foreach ($result as $item){?>
        <tr>
            <td><?=$item['id']?></td>
            <td><?=$item['good_name']?></td>
            <td><img src="<?=$item['img']?>" height="100px"></td>
            <td><?=$item['class_name']?></td>
            <td><?=$item['style']==1?'在售':($item['style']==2?'售罄':'异常')?></td>
            <td><?=$item['create_time']?></td>
            <td><?=$item['update_time']?></td>
            <td><a href="update.php?id=<?=$item['id']?>">修改</a>----<a href="delete.php?id=<?=$item['id']?>">删除</a></td>
        </tr>
    <? }?>


    <p>
        <a href="<?= $firstpage?>">首页</a>
        <a href="<?= $prepage?>">上一页</a>
        <a href="<?= $nextpage?>">下一页</a>
        <a href="<?= $lastpage?>">尾页</a>
    </p>
</body>
</html>
